<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:CustEvent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:CustEvent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:CustEvent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:CustEvent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CustEventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件编号" align="center" prop="eventid" />
      <el-table-column label="客户编号" align="center" prop="custid" />
      <el-table-column label="成交几率" align="center" prop="salepercent" />
      <el-table-column label="机会阶段" align="center" prop="chance" />
      <el-table-column label="事件类型" align="center" prop="eventtypeid" />
      <el-table-column label="事件时间" align="center" prop="eventtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eventtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:CustEvent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:CustEvent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户事件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="成交几率" prop="salepercent">
           <el-select v-model="form.salepercent" placeholder="请选择成交几率">
                <el-option
                v-for="item in salePercents"
                :key="item.salePercent"
                :label="item.label"
                :value="item.salePercent">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="机会阶段" prop="chance">
            <el-select v-model="form.chance" placeholder="请选择就会阶段">
            <el-option
                v-for="item in chances"
                :key="item.chance"
                :label="item.label"
                :value="item.chance">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="事件类型" prop="eventtypeid">
          <el-select v-model="form.eventtypeid" placeholder="请选择事件类型"  >
            <el-option 
                v-for="type in EventTypeList"
                :key="type.eventtypename"
                :label="type.eventtypename"
                :value="type.eventtypename">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="事件时间" prop="eventtime">
          <el-date-picker clearable
            v-model="form.eventtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择事件时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustEvent, getCustEvent, delCustEvent, addCustEvent, updateCustEvent } from "@/api/house_sale/CustEvent";
import {listEventType}  from "@/api/house_sale/EventType";

export default {
  name: "CustEvent",
  data() {
    return {

         salePercents: [{
             salePercent:'0%',
             label:'0%'
        },{
             salePercent:'10%',
             label:'10%'
        },{
             salePercent:'20%',
             label:'20%'
        },{
             salePercent:'30%',
             label:'30%'
        },{
             salePercent:'40%',
             label:'40%'
        },{
             salePercent:'50%',
             label:'50%'
        },{
             salePercent:'60%',
             label:'60%'
        },{
             salePercent:'70%',
             label:'70%'
        },{
             salePercent:'80%',
             label:'80%'
        },{
             salePercent:'90%',
             label:'90%'
        },{
             salePercent:'100%',
             label:'100%'
        }],
        salePercent:'',
        chances: [{
            chance:'比较',
            label:'比较'
        },{
             chance:'兴趣',
             label:'兴趣'
        },{
             chance:'酝酿',
             label:'酝酿'
        }],
        chance:'',
      // 事件类型表格数据
      EventTypeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户事件表格数据
      CustEventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custid:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eventtime: [
          { required: true, message: "事件时间不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户事件列表 */
    getList() {
      this.loading = true;
      this.queryParams.custid=this.$route.query.id;
      listCustEvent(this.queryParams).then(response => {
        this.CustEventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
       listEventType(this.queryParam).then(response =>{
         this.EventTypeList=response.rows;
     });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      const custid=this.$route.query.id;
      console.log(custid);
      this.form = {
        eventid: null,
        custid: custid,
        salepercent: null,
        chance: null,
        eventtypeid: null,
        eventtime: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eventid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户事件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const eventid = row.eventid || this.ids
      getCustEvent(eventid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户事件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.eventid != null) {
            updateCustEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eventids = row.eventid || this.ids;
      this.$modal.confirm('是否确认删除客户事件编号为"' + eventids + '"的数据项？').then(function() {
        return delCustEvent(eventids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/CustEvent/export', {
        ...this.queryParams
      }, `CustEvent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
